package com.jinlejia.bbs.adapter;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.bean.base.TagBean;
import com.jinlejia.bbs.bean.base.TagGroupBean;
import com.jinlejia.bbs.databinding.BbsAdapterTagBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王进 on 2019/3/11.
 */

public class TagAdapter extends RecyclerView.Adapter {
    private final Context context;
    private final List<TagGroupBean> datas;


    public TagAdapter(Context context, List<TagGroupBean> beans) {
        this.context = context;
        this.datas = beans;
    }

    public interface OnChangeListener{
        void onChange(TagAdapter adapter);
    }
    private OnChangeListener listener;

    public void setListener(OnChangeListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.bbs_adapter_tag, parent, false);
        return new TagListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        BbsAdapterTagBinding binding = DataBindingUtil.bind(holder.itemView);
        TagGroupBean bean = datas.get(position);
        binding.tagGroupNameTextView.setText(bean.getGroupName());
        binding.flexBoxLayout.removeAllViews();
        for (TagBean tag : bean.getTags()) {
            TextView textView = new TextView(binding.getRoot().getContext());
            textView.setText(tag.getValueName());
            textView.setBackgroundColor(Color.TRANSPARENT);
            if (tag.isSelected()) {
                textView.setBackgroundResource(R.drawable.bbs_shape_yeallw_round);
                textView.setTextColor(Color.parseColor("#FEFEFE"));
            } else {
                textView.setBackgroundResource(R.drawable.bbs_shape_gray_round);
                textView.setTextColor(Color.parseColor("#777777"));
            }
            textView.setTag(tag);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TagBean tag = (TagBean) v.getTag();
                    tag.setSelected(!tag.isSelected());
                    notifyDataSetChanged();
                    if (listener!=null){
                        listener.onChange(TagAdapter.this);
                    }
                }
            });
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            binding.flexBoxLayout.addView(textView);
        }
    }

    public ArrayList<TagBean> getSelectTags() {
        ArrayList<TagBean> selectTags = new ArrayList<>();
        for (TagGroupBean bean:datas) {
            ArrayList<TagBean> tags = bean.getTags();
            for (TagBean tag:tags) {
                if (tag.isSelected()){
                    selectTags.add(tag);
                }
            }
        }
        return selectTags;
    }



    @Override
    public int getItemCount() {
        return datas.size();
    }

    class TagListViewHolder extends RecyclerView.ViewHolder {

        TagListViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
}
